<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Services\WxTemplatemsg\WxTemplatemsgService;

/**
 * 微信模板消息管理
 * @package App\Http\Controllers\Admin
 */
class WxTemplatemsgController extends BaseWxController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 模板列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request, WxTemplatemsgService $templatemsgSer)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxTemplatemsg::select('*');
            //当前选择微信应用数据
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //关键字搜索
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('title', 'like', '%'.$keyword.'%');
            }
            $data = $query->paginate($pageSize);
            $data = !empty($data) ? $data->toArray() : [];
            if ($data['data']) {
                $list = $data['data'];
                foreach ($list as $k => $v) {
                    $v['template_key_str'] = $templatemsgSer->getKeyName($v['template_key']);
                    $list[$k] = $v;
                }
                $data['data'] = $list;
            }
            return $data;
        }
        //行业列表
        $this->viewData['industryList'] = $templatemsgSer->getIndustryList();
        //当前所属行业
        $this->viewData['industrySet'] = $templatemsgSer->getIndustry();
        //模板key列表
        $this->viewData['keyList'] = $templatemsgSer->getKeyList();
        return view('admin.wxtemplatemsg.index', $this->viewData);
    }

    /**
     * 新增模板
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request, WxTemplatemsgService $templatemsgSer)
    {
        try {
            $templatemsgSer->add($request);
            return $this->showMsg('success', '添加模板成功', [], 'admin.wxtemplatemsg.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 编辑模板
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, WxTemplatemsgService $templatemsgSer)
    {
        try {
            $templatemsgSer->edit($request);
            return $this->showMsg('success', '修改模板成功', [], 'admin.wxtemplatemsg.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 设置行业
     * @param Request $request
     * @return mixed
     */
    public function setIndustry(Request $request, WxTemplatemsgService $templatemsgSer)
    {
        try {
            $templatemsgSer->setIndustry($request);
            return $this->showMsg('success', '设置行业成功', [], 'admin.wxtemplatemsg.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除模板
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, WxTemplatemsgService $templatemsgSer)
    {
        try {
            $templatemsgSer->del($request->id ?? 0);
            return $this->showMsg('success', '删除模板成功', [], 'admin.wxtemplatemsg.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 同步模板
     * @return \Illuminate\Http\Response
     */
    public function sync(WxTemplatemsgService $templatemsgSer)
    {
        try {
            $templatemsgSer->sync();
            return $this->showMsg('success', '同步模板消息成功', [], 'admin.wxtemplatemsg.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 模板消息日志
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function log(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxTemplatemsgLog::select('*');
            //模板KEY
            $template_key = $request->get('template_key', '');
            if ($template_key){
                $query->where('template_key', $template_key);
            }
            //接收者
            $touser = $request->get('touser', '');
            if ($touser){
                $query->where('touser', $touser);
            }
            //模板ID
            $template_id = $request->get('template_id', '');
            if ($template_id){
                $query->where('template_id', $template_id);
            }
            $list = $query->paginate($pageSize);
            if ($list){
                foreach ($list as $k => $v){
                    $v['content_str'] = var_export($v->content, true);
                    $list[$k] = $v;
                }
            }
            return $list;
        }
        //二维码分组数据
        $groupList = (new \App\Services\WxQrcode\WxQrcodeGroupService())->getGroupList();
        $this->viewData['groupList'] = $groupList;
        return view('admin.wxtemplatemsg.log', $this->viewData);
    }
}
